<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>指定需要被序列化成 JSON 的数据</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.jsonserializable.html">JsonSerializable</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.json.html">JSON 函数</a></div>
 <div class="up"><a href="class.jsonserializable.html">JsonSerializable</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="jsonserializable.jsonserialize" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">JsonSerializable::jsonSerialize</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0)</p><p class="refpurpose"><span class="refname">JsonSerializable::jsonSerialize</span> &mdash; <span class="dc-title">指定需要被序列化成 JSON 的数据</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-jsonserializable.jsonserialize-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">abstract</span> <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>JsonSerializable::jsonSerialize</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <p class="para rdfs-comment">
   序列化物体（Object）成能被 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 原生地序列化的值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-jsonserializable.jsonserialize-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-jsonserializable.jsonserialize-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回能被 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 序列化的数据，
   这个值可以是除了 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> 外的任意类型。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-jsonserializable.jsonserialize-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3658">
    <p><strong>Example #1 
     <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
     returning an <span class="type"><a href="language.types.array.html" class="type array">array</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ArrayValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ArrayValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
[
    1,
    2,
    3
]
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3659">
    <p><strong>Example #2 
     <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
     返回了一个关联数组 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ArrayValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'quux'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ArrayValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
{
    &quot;foo&quot;: &quot;bar&quot;,
    &quot;quux&quot;: &quot;baz&quot;
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3660">
    <p><strong>Example #3 
     <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
     返回一个 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IntegerValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">number&nbsp;</span><span style="color: #007700">=&nbsp;(integer)&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">number</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">IntegerValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3661">
    <p><strong>Example #4 
     <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
     返回一个 <span class="type"><a href="language.types.string.html" class="type string">string</a></span>
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">StringValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">string&nbsp;</span><span style="color: #007700">=&nbsp;(string)&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">StringValue</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello!'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&quot;Hello!&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.jsonserializable.html">JsonSerializable</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.json.html">JSON 函数</a></div>
 <div class="up"><a href="class.jsonserializable.html">JsonSerializable</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
